package application.mvc.sys.config.fault;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;


import application.common.model.FaultBox;
import application.common.render.FaultBoxProperty;
import application.common.ui.FaultBoxDialog;
import application.common.ui.ProgressStage;
import application.common.util.LayerUtil;
import application.common.util.RouteUtil;
import application.common.util.ShiroUtil;
import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.util.Duration;

public class FaultController implements Initializable{

	@FXML
	private Label headerLabel;
	
	@FXML
	private Label copyrightLabel;
	
	@FXML
	private Label usernameLabel;
	
	@FXML
	private Label roleNameLabel;
	
	@FXML
	private ImageView portraitImageView;

	@FXML
	private TableView<FaultBoxProperty> faultTableView;
	
	@FXML
	private TextField keywordTextField;
	
	
	@FXML
	private Pane timerOutLinePane;

	@FXML
	private Pane timerInLinePane;

	@FXML
	private Label timerLabel;
	private Timeline animation;
	private RotateTransition rotateTransitionOut;
	private RotateTransition rotateTransitionIn;

	private final ObservableList<FaultBoxProperty> data = FXCollections.observableArrayList();
	private boolean isDestroy=false;
	@SuppressWarnings("unchecked")
	public void initialize(URL location, ResourceBundle resources) {
		
		headerLabel.setText(ShiroUtil.getSoftName());
		copyrightLabel.setText(ShiroUtil.getCopyright());
		
		if(ShiroUtil.getUser()!=null) {
			portraitImageView.setImage(ShiroUtil.getUser().getPortrait());
			usernameLabel.setText(ShiroUtil.getUser().getName());
			roleNameLabel.setText(ShiroUtil.getUser().getRoleName());
		}

		TableColumn<FaultBoxProperty, String> idColumn = new TableColumn<FaultBoxProperty, String>("主键");
		idColumn.setMinWidth(60);
		idColumn.setCellValueFactory(new PropertyValueFactory<FaultBoxProperty, String>("id"));
		idColumn.setVisible(false);
		
		TableColumn<FaultBoxProperty, String> cabinetNameColumn = new TableColumn<FaultBoxProperty, String>("名称");
		cabinetNameColumn.setMinWidth(150);
		cabinetNameColumn.setCellValueFactory(new PropertyValueFactory<FaultBoxProperty, String>("cabinetName"));

		TableColumn<FaultBoxProperty, String> boardNameColumn = new TableColumn<FaultBoxProperty, String>("板卡");
		boardNameColumn.setMinWidth(250);
		boardNameColumn.setCellValueFactory(new PropertyValueFactory<FaultBoxProperty, String>("boardName"));

		TableColumn<FaultBoxProperty, String> boxNoColumn = new TableColumn<FaultBoxProperty, String>("柜号");
		boxNoColumn.setMinWidth(250);
		boxNoColumn.setCellValueFactory(new PropertyValueFactory<FaultBoxProperty, String>("boxNo"));
		
		faultTableView.getColumns().addAll(idColumn,cabinetNameColumn, boardNameColumn, boxNoColumn);
		faultTableView.setItems(data);
		
		ProgressStage.of(RouteUtil.mainStage, new Task<Void>() {
			@Override
			protected Void call() throws Exception {
				Platform.runLater(new Runnable() {
					public void run() {
						List<FaultBox> faultBoxs = FaultBox.dao.find(" select * from  "+ FaultBox.TABLE_NAME);
						if (faultBoxs != null && faultBoxs.size() > 0) {
							for (FaultBox faultBox : faultBoxs) {
								data.add(new FaultBoxProperty(faultBox));
							}
						}
						faultTableView.setItems(data);
						if(!isDestroy) {
							rotateTransitionOut = new RotateTransition(Duration.seconds(10), timerOutLinePane);
							rotateTransitionOut.setFromAngle(0);
							rotateTransitionOut.setToAngle(360);
							rotateTransitionOut.setByAngle(360); // 设置旋转的角度
							rotateTransitionOut.setCycleCount(Animation.INDEFINITE); // 设置旋转次数，我们需要旋转无数次
							rotateTransitionOut.setInterpolator(Interpolator.LINEAR);
							rotateTransitionOut.play();

							rotateTransitionIn = new RotateTransition(Duration.seconds(10), timerInLinePane);
							rotateTransitionIn.setFromAngle(360);
							rotateTransitionIn.setToAngle(0);
							rotateTransitionIn.setByAngle(360); // 设置旋转的角度
							rotateTransitionIn.setCycleCount(Animation.INDEFINITE); // 设置旋转次数，我们需要旋转无数次
							rotateTransitionIn.setInterpolator(Interpolator.LINEAR);
							rotateTransitionIn.play();
							startCountDown();
						}
					}
				});
				return null;
			}
		}, "查询中...").show();
	}
	
	
	public void startCountDown() {
		animation = new Timeline(new KeyFrame(Duration.millis(1000), new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				if(RouteUtil.getTime()>0) {
					RouteUtil.setTime(RouteUtil.getTime() - 1);
					if (RouteUtil.getTime() == 0) {
						timerLabel.setText("0");
						LayerUtil.open("操作超时！", 999);
					} else if (RouteUtil.getTime() > 0) {
						timerLabel.setText(RouteUtil.getTime() + "");
					}
				}
				
			}
		}));
		animation.setCycleCount(Timeline.INDEFINITE);
		animation.play();
	}

	public void destroy() {
		if (animation != null) {
			animation.stop();
		}
		if (rotateTransitionOut != null) {
			rotateTransitionOut.stop();
		}
		if (rotateTransitionIn != null) {
			rotateTransitionIn.stop();
		}
	}

	public void exit(ActionEvent event) {
		if(!isDestroy) {
			isDestroy=true;
			Button bu = (Button) event.getSource();
			bu.setVisible(false);
			destroy();
			RouteUtil.goConfig();
		}
	}
	
	
	public void delete(ActionEvent event) {
		new FaultBoxDialog();
	}
	

}
